<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>二叉树</title>
</head>
<body>
    <h2>二叉树</h2>

    <script>
        let BinaryTree = function(key){
            let tNode = function(key){
                this.key = key;
                this.left = null;
                this.right = null;
            };
            let root = null, arr = [];
            let insertNode = function(newNode){
                if(arr[0].left == null){
                    arr[0].left = newNode
                }else if(arr[0].right == null){
                    arr[0].right = newNode;
                    arr.shift();
                }
                //console.log('newNode', newNode)
                arr.push(newNode);
                console.log('arr', arr)
            };
            this.insert = function(key){
                let newNode = new tNode(key);
                if(root == null){
                    root = newNode;
                    arr.push(root);
                }else{
                    insertNode(newNode);
                }
            }
        };
        let nodes = [1,2,3,4,5,6,7,8];
        let binaryTree = new BinaryTree();
        nodes.forEach((key)=>{
            binaryTree.insert(key);
        });
        console.log('binaryTree', binaryTree)
    </script>
</body>
</html>